/*
Matthew Parker
CPE 301
Design Assignment 4
1. Generate 3 PWMs to drive an RGB LED using Timer0 and Timer2
2. Increment each PWM's period and duty cycle from min to max
3. Use timer1 to hold each color for a specific time period
*/

#define F_CPU 8000000L
//#define XTAL 16000000L
#include <inttypes.h>
#include <avr/io.h>
#include <util/delay.h>
#define byte volatile unsigned char

void delay5ms(void);

int main(void)
{
	DDRD = (1<<PORTD3) | (1<<PORTD5) | (1<<PORTD6); //PD3=OC2B, PDPD5=OC0B, PD6=OC0A
    byte i = 0;
	TCCR0A = (1<<WGM00) | (1<<WGM01) | (1<<COM0B1) | (1<<COM0A1); // Fast PWM, non inverting
	TCCR0B = (1<<WGM02) | (1<<CS01); // no prescalar
	TCCR2A = (1<<WGM20) | (1<<WGM21) | (1<<COM2B1); // Fast PWM, non inverting
	TCCR2B = (1<<WGM22) | (1<<CS21); // no prescalar
	while(1){
	for (i=20; i < 230; i++){
		OCR0A = i;
		delay5ms();
		//_delay_ms(5);
	}
	OCR0A = 0;
	
	for (i=20; i < 230; i++){
		OCR0B = i;
		delay5ms();
		//_delay_ms(5);
	}
	OCR0B = 0;
	
	for (i=20; i < 230; i++){
		OCR2B = i;
		delay5ms();
		//_delay_ms(5);
	}
	OCR2B = 0;
	}
	return 0;
}

void delay5ms(void){
	//count to 40000 at 8MHz for 5ms delay
	//40000 = 9C40
	//TCCR1B = (1<<CS10); //normal mode, no prescalar
	TCNT1 = 0;
	while(TCNT1 < 0x9C40){
		TCNT1++;
	}
}